﻿using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using RibbonLib;
using todo.Plugin;
using todo.Properties;
using todo.Ribbon;
using todo.Ribbon.CmdUI;

namespace todo
{
    partial class FrmMain
    {
        private RibbonUI _ribbonUi;

        private RibbonButton _btnUndo;
        private RibbonButton _btnRedo;
        private RibbonButton _btnClassicUI;
        private RibbonInRibbonGallery _tagsGallery;
        private RibbonToggleButton _btnActive;
        private RibbonToggleButton _btnCheck;
        private RibbonRecentItems _recentItems;
        private RibbonToggleButton[] _priorityButtons;
        private RibbonColorPicker _colorPicker;
        private RibbonToggleButton _btnBold;
        private RibbonToggleButton _btnItalic;
        private RibbonToggleButton _btnUnderline;
        private RibbonToggleButton _btnEdit;
        private RibbonButton _btnExit;

        private RibbonButton _btnAddBefore;
        private RibbonButton _btnAddAfter;
        private RibbonButton _btnAddChild;
        private RibbonButton _btnDelete;

        private RibbonButton _btnNew;
        private RibbonButton _btnOpen;
        private RibbonButton _btnSave;
        private RibbonButton _btnSaveAs;
        private RibbonButton _btnProperties;
        private RibbonButton _btnAbout;
        private RibbonButton _btnZoomIn;
        private RibbonButton _btnZoomOut;
        private RibbonButton _btnZoomReset;
        private RibbonToggleButton _btnShowProgress;
        private RibbonToggleButton _btnShowDue;
        private RibbonToggleButton _btnShowDoneItems;

        private RibbonCheckBox _btnShowGridLine;
        private RibbonCheckBox _btnShowTreeLine;
        private RibbonCheckBox _btnShowHeader;

        private RibbonButton _btnCut;
        private RibbonButton _btnCopy;

        private RibbonControl _btnPasteSplit;
        private RibbonButton _btnPaste;
        private RibbonButton _btnPasteAfter;
        private RibbonButton _btnPasteChild;

        private RibbonButton _btnExpandAll;
        private RibbonButton _btnCollapseAll;

        private RibbonButton _btnDebug;
        private RibbonButton _btnOptions;
        private RibbonButton _btnExport;
        private RibbonButton _btnArchive;

        private RibbonButton _btnHelp;
        private RibbonDropDownGallery _themeGallery;

        private RibbonButton _btnScheduler;
        private RibbonButton _btnSetReminder;

        private RibbonButton _btnPrint;
        private RibbonButton _btnPrintSetup;
        private RibbonButton _btnPrintPreview;


        private void InitRibbonUI()
        {
            this._ribbonUi = new RibbonUI(this);
            _ribbonUi.OnInited += _ribbonUi_OnInited;
        }

        private void _ribbonUi_OnInited()
        {
            //ttt
            UpdateRibbonTheme();

            //
            _btnUndo = new RibbonButton(_ribbonUi, 1030) {Enabled = false};
            App.CmdUIManager.Bind(App.Cmds.Undo, new RibbonButtonUI(_btnUndo));

            _btnRedo = new RibbonButton(_ribbonUi, 1040) {Enabled = false};
            App.CmdUIManager.Bind(App.Cmds.Redo, new RibbonButtonUI(_btnRedo));

            //
            _btnActive = new RibbonToggleButton(_ribbonUi, 1100);
            App.CmdUIManager.Bind(App.Cmds.Active, new RibbonToggleButtonUI(_btnActive));

            _btnCheck = new RibbonToggleButton(_ribbonUi, 1110);
            App.CmdUIManager.Bind(App.Cmds.Check, new RibbonToggleButtonUI(_btnCheck));

            //
            _recentItems = new RibbonRecentItems(_ribbonUi, 900);
            SetupRecentItems();
            _recentItems.OnPinStateChanged += item => {
                if (item.Pinned)
                {
                    _mru.Pin(item.LabelDescription);
                }
                else
                {
                    _mru.Unpin(item.LabelDescription);
                }
            };
            _recentItems.OnClick += _recentItems_OnClick;

            //
            _btnClassicUI = new RibbonButton(_ribbonUi, 2000);
            _btnClassicUI.OnClick += (sender, args) => {
                InitClassicUI();
                this.Menu = this.mainMenu1;
                _ribbonUi.Hide();
            };

            //
            _themeGallery = new RibbonDropDownGallery(_ribbonUi, 2005);
            var items = new List<GalleryItemPropertySet>();

            for (var n = 0; n < Consts.ThemeNames.Length; n++)
            {
                var themeName = Consts.ThemeNames[n];
                var themeLabel = Consts.ThemeLabels[n];
                var bmp = (Bitmap) Resources.ResourceManager.GetObject(themeName + "_theme");
                items.Add(new GalleryItemPropertySet {
                        Label =
                                themeLabel.Substring(0, 1).ToUpperInvariant() +
                                themeLabel.Substring(1),
                        //ItemImage = _ribbonUi.ConvertToUIImage((Bitmap)imlThemes.Images[themeName])
                        ItemImage = _ribbonUi.ConvertToUIImage(bmp)
                });
            }

            _themeGallery.Items = items;
            App.CmdUIManager.Bind(App.Cmds.Theme, new RibbonGalleryUI(_themeGallery));

            //
            _tagsGallery = new RibbonInRibbonGallery(_ribbonUi, 920);
            items = new List<GalleryItemPropertySet>();

            for (var n = 0; n < Consts.TagNames.Length; n++)
            {
                var tagName = Consts.TagNames[n];
                var tagLabel = Consts.TagLabels[n];
                items.Add(new GalleryItemPropertySet {
                        Label = tagLabel.Substring(0, 1).ToUpperInvariant() + tagLabel.Substring(1),
                        ItemImage =
                                _ribbonUi.ConvertToUIImage((Bitmap) imlTags.Images[tagName + ".png"])
                });
            }
            _tagsGallery.Items = items;
            App.CmdUIManager.Bind(App.Cmds.Tag, new RibbonGalleryUI(_tagsGallery));

            //
            var _priorityGallery = new RibbonDropDownGallery(_ribbonUi, 1300);
            _priorityButtons = new RibbonToggleButton[6];
            for (var n = 0; n <= 5; n++)
            {
                _priorityButtons[n] = new RibbonToggleButton(_ribbonUi, (uint) (1310 + n));
            }
            App.CmdUIManager.Bind(App.Cmds.Priority,
                    new RibbonToggleButtonGroupUI(_priorityGallery, _priorityButtons));

            //
            _colorPicker = new RibbonColorPicker(_ribbonUi, 1170);
            _colorPicker.StandardColors = new[] {
                    Color.FromArgb(192, 0, 0),
                    Color.FromArgb(255, 0, 0),
                    Color.FromArgb(255, 192, 0),
                    Color.FromArgb(255, 255, 0),
                    Color.FromArgb(146, 208, 80),
                    Color.FromArgb(0, 176, 80),
                    Color.FromArgb(0, 176, 240),
                    Color.FromArgb(0, 112, 192),
                    Color.FromArgb(0, 32, 96),
                    Color.FromArgb(0, 32, 96),
                    Color.FromArgb(250, 250, 210), //
                    Color.FromArgb(242, 222, 222),
                    Color.FromArgb(223, 240, 216),
                    Color.FromArgb(196, 227, 243),
                    Color.FromArgb(224, 255, 255),
                    Color.FromArgb(249, 249, 249),
                    Color.FromArgb(255, 255, 183)
            };
            App.CmdUIManager.Bind(App.Cmds.Color, new RibbonColorPickerUI(_colorPicker));

            //
            _btnBold = new RibbonToggleButton(_ribbonUi, 1140);
            App.CmdUIManager.Bind(App.Cmds.Bold, new RibbonToggleButtonUI(_btnBold));

            _btnItalic = new RibbonToggleButton(_ribbonUi, 1150);
            App.CmdUIManager.Bind(App.Cmds.Italic, new RibbonToggleButtonUI(_btnItalic));

            _btnUnderline = new RibbonToggleButton(_ribbonUi, 1160);
            App.CmdUIManager.Bind(App.Cmds.Underline, new RibbonToggleButtonUI(_btnUnderline));

            //
            _btnEdit = new RibbonToggleButton(_ribbonUi, 1130);
            App.CmdUIManager.Bind(App.Cmds.Edit, new RibbonToggleButtonUI(_btnEdit));

            //
            _btnExit = new RibbonButton(_ribbonUi, 1010);
            App.CmdUIManager.Bind(App.Cmds.Exit, new RibbonButtonUI(_btnExit));

            //
            _btnAddBefore = new RibbonButton(_ribbonUi, 1120);
            App.CmdUIManager.Bind(App.Cmds.AddBefore, new RibbonButtonUI(_btnAddBefore));

            _btnAddAfter = new RibbonButton(_ribbonUi, 1121);
            App.CmdUIManager.Bind(App.Cmds.AddAfter, new RibbonButtonUI(_btnAddAfter));

            _btnAddChild = new RibbonButton(_ribbonUi, 1122);
            App.CmdUIManager.Bind(App.Cmds.AddChild, new RibbonButtonUI(_btnAddChild));

            _btnDelete = new RibbonButton(_ribbonUi, 1240);
            App.CmdUIManager.Bind(App.Cmds.Delete, new RibbonButtonUI(_btnDelete));

            //
            _btnNew = new RibbonButton(_ribbonUi, 1180);
            App.CmdUIManager.Bind(App.Cmds.New, new RibbonButtonUI(_btnNew));

            _btnOpen = new RibbonButton(_ribbonUi, 1190);
            App.CmdUIManager.Bind(App.Cmds.Open, new RibbonButtonUI(_btnOpen));

            _btnSave = new RibbonButton(_ribbonUi, 1200);
            App.CmdUIManager.Bind(App.Cmds.Save, new RibbonButtonUI(_btnSave));

            _btnSaveAs = new RibbonButton(_ribbonUi, 1210);
            App.CmdUIManager.Bind(App.Cmds.SaveAs, new RibbonButtonUI(_btnSaveAs));

            _btnProperties = new RibbonButton(_ribbonUi, 1220);
            App.CmdUIManager.Bind(App.Cmds.Properties, new RibbonButtonUI(_btnProperties));

            _btnAbout = new RibbonButton(_ribbonUi, 1230);
            App.CmdUIManager.Bind(App.Cmds.About, new RibbonButtonUI(_btnAbout));

            //
            _btnZoomIn = new RibbonButton(_ribbonUi, 2020);
            App.CmdUIManager.Bind(App.Cmds.ZoomIn, new RibbonButtonUI(_btnZoomIn));

            _btnZoomOut = new RibbonButton(_ribbonUi, 2030);
            App.CmdUIManager.Bind(App.Cmds.ZoomOut, new RibbonButtonUI(_btnZoomOut));

            _btnZoomReset = new RibbonButton(_ribbonUi, 2040);
            App.CmdUIManager.Bind(App.Cmds.ZoomReset, new RibbonButtonUI(_btnZoomReset));

            //
            _btnShowProgress = new RibbonToggleButton(_ribbonUi, 2010);
            App.CmdUIManager.Bind(App.Cmds.ShowProgress, new RibbonToggleButtonUI(_btnShowProgress));

            _btnShowDue = new RibbonToggleButton(_ribbonUi, 2011);
            App.CmdUIManager.Bind(App.Cmds.ShowDue, new RibbonToggleButtonUI(_btnShowDue));

            _btnShowDoneItems = new RibbonToggleButton(_ribbonUi, 2015);
            App.CmdUIManager.Bind(App.Cmds.ShowDoneItems,
                    new RibbonToggleButtonUI(_btnShowDoneItems));

            _btnShowGridLine = new RibbonCheckBox(_ribbonUi, 2070);
            App.CmdUIManager.Bind(App.Cmds.ShowGridLine, new RibbonCheckBoxUI(_btnShowGridLine));

            _btnShowTreeLine = new RibbonCheckBox(_ribbonUi, 2080);
            App.CmdUIManager.Bind(App.Cmds.ShowTreeLine, new RibbonCheckBoxUI(_btnShowTreeLine));

            _btnShowHeader = new RibbonCheckBox(_ribbonUi, 2090);
            App.CmdUIManager.Bind(App.Cmds.ShowHeader, new RibbonCheckBoxUI(_btnShowHeader));

            //
            _btnCut = new RibbonButton(_ribbonUi, 1050);
            App.CmdUIManager.Bind(App.Cmds.Cut, new RibbonButtonUI(_btnCut));

            _btnCopy = new RibbonButton(_ribbonUi, 1060);
            App.CmdUIManager.Bind(App.Cmds.Copy, new RibbonButtonUI(_btnCopy));

            //
            _btnPasteSplit = new RibbonControl(_ribbonUi, 1090);
            _btnPaste = new RibbonButton(_ribbonUi, 1070);
            App.CmdUIManager.Bind(App.Cmds.Paste, new RibbonButtonUI(_btnPaste));
            App.CmdUIManager.Bind(App.Cmds.Paste, new RibbonControlUI(_btnPasteSplit));

            _btnPasteAfter = new RibbonButton(_ribbonUi, 1071);
            App.CmdUIManager.Bind(App.Cmds.PasteAfter, new RibbonButtonUI(_btnPasteAfter));

            _btnPasteChild = new RibbonButton(_ribbonUi, 1072);
            App.CmdUIManager.Bind(App.Cmds.PasteChild, new RibbonButtonUI(_btnPasteChild));

            //
            _btnExpandAll = new RibbonButton(_ribbonUi, 2050);
            App.CmdUIManager.Bind(App.Cmds.ExpandAll, new RibbonButtonUI(_btnExpandAll));

            _btnCollapseAll = new RibbonButton(_ribbonUi, 2060);
            App.CmdUIManager.Bind(App.Cmds.CollapseAll, new RibbonButtonUI(_btnCollapseAll));

            //
            _btnOptions = new RibbonButton(_ribbonUi, 3000);
            App.CmdUIManager.Bind(App.Cmds.Options, new RibbonButtonUI(_btnOptions));

            _btnExport = new RibbonButton(_ribbonUi, 3010);
            App.CmdUIManager.Bind(App.Cmds.Export, new RibbonButtonUI(_btnExport));

            _btnArchive = new RibbonButton(_ribbonUi, 3020);
            App.CmdUIManager.Bind(App.Cmds.Archive, new RibbonButtonUI(_btnArchive));

            _btnScheduler = new RibbonButton(_ribbonUi, 3030);
            App.CmdUIManager.Bind(App.Cmds.Scheduler, new RibbonButtonUI(_btnScheduler));

            _btnSetReminder = new RibbonButton(_ribbonUi, 4010);
            App.CmdUIManager.Bind(App.Cmds.SetReminder, new RibbonButtonUI(_btnSetReminder));

            _btnPrint = new RibbonButton(_ribbonUi, 4030);
            App.CmdUIManager.Bind(App.Cmds.Print, new RibbonButtonUI(_btnPrint));

            _btnPrintSetup = new RibbonButton(_ribbonUi, 4040);
            App.CmdUIManager.Bind(App.Cmds.PrintSetup, new RibbonButtonUI(_btnPrintSetup));

            _btnPrintPreview = new RibbonButton(_ribbonUi, 4050);
            App.CmdUIManager.Bind(App.Cmds.PrintPreview, new RibbonButtonUI(_btnPrintPreview));

            //
            _btnHelp = new RibbonButton(_ribbonUi, 910);
            App.CmdUIManager.Bind(App.Cmds.Help, new RibbonButtonUI(_btnHelp));

            if (Debugger.IsAttached)
            {
                _btnDebug = new RibbonButton(_ribbonUi, 3090);
                App.CmdUIManager.Bind(App.Cmds.Debug, new RibbonButtonUI(_btnDebug));
            }

            //
            PluginManager.Instance.OnRibbonInited(_ribbonUi);
        }

        public void UpdateRibbonTheme()
        {
            var themeName = App.Settings.ThemeName;
            if (themeName == "green")
            {
                this._ribbonUi.SetColors(Color.FromArgb(219, 235, 208), Color.Yellow, Color.Black);
            }
            else if (themeName == "chinese")
            {
                this._ribbonUi.SetColors(Color.FromArgb(252, 240, 218),
                        Color.FromArgb(254, 145, 122), Color.Black);
            }
            else
            {
                this._ribbonUi.ResetColors();
            }
        }

        private void LoadRibbonSettings()
        {
            var modes = new List<byte>();
            modes.Add(0);
            if (Debugger.IsAttached)
            {
                modes.Add(1);
            }
            if (PluginManager.Instance.ContainsPlugin("gtasks"))
            {
                modes.Add(2);
            }
            _ribbonUi.SetModes(modes.ToArray());

            var ribbonSettings = App.Settings["Ribbon"]["LastState"].BytesValue;
            if (ribbonSettings == null || ribbonSettings.Length == 0)
            {
                return;
            }
            var ms = new MemoryStream(ribbonSettings);
            _ribbonUi.LoadSettingsFromStream(ms);
            ms.Dispose();
        }


        private bool ShowingRibbon
        {
            get { return _ribbonUi != null && _ribbonUi.Loaded && !_ribbonUi.Hidden; }
        }


        private void SaveRibbonSettings()
        {
            var ms = new MemoryStream();
            _ribbonUi.SaveSettingsToStream(ms);
            App.Settings["Ribbon"]["LastState"].BytesValue = ms.ToArray();
            ms.Dispose();
        }

        public bool ShowContextMenu(int x, int y)
        {
            if (!ShowingRibbon)
            {
                return false;
            }

            if (www.Zoom != 100)
            {
                x = (int) (x*www.Zoom/100.0);
                y = (int) (y*www.Zoom/100.0);
            }
            var pt = new Point(x, y);
            pt = www.PointToScreen(pt);
            //            Win32.GetCursorPos(out pt);
            _ribbonUi.ShowContextPopup(990, pt.X, pt.Y);

            return true;
        }

        private void _recentItems_OnClick(RecentItemsPropertySet item)
        {
            var labelDescription = item.LabelDescription;

            //
            var filename = labelDescription;
            if (!File.Exists(filename))
            {
                OnMruItemNotFound(filename);
            }
            else
            {
                OnOpen(labelDescription);
            }
        }
    }
}